﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace VAdvantage.Controls
{
    [TemplatePart(Name = AutoCompleteComboBox.ElementToggleButton, Type = typeof(ToggleButton))]
    public partial class AutoCompleteComboBox : AutoCompleteBox
    {
        private const string ElementToggleButton = "ToggleButton";
        //object _Selection;

        #region Template Parts

        ToggleButton _ToggleButton;

        ToggleButton ToggleButton
        {
            get { return _ToggleButton; }
            set
            {
                if (_ToggleButton != null)
                {
                    _ToggleButton.Click -= new RoutedEventHandler(ToggleButton_Click);
                    _ToggleButton.MouseEnter -= new MouseEventHandler(ToggleButton_MouseEnter);
                    _ToggleButton.MouseLeave -= new MouseEventHandler(ToggleButton_MouseLeave);
                }

                _ToggleButton = value;

                if (_ToggleButton != null)
                {
                    _ToggleButton.Click += new RoutedEventHandler(ToggleButton_Click);
                    _ToggleButton.MouseEnter += new MouseEventHandler(ToggleButton_MouseEnter);
                    _ToggleButton.MouseLeave += new MouseEventHandler(ToggleButton_MouseLeave);
                }
            }
        }

        #endregion

        //Commented code was originally added to try to handle SelectedValue type functionality
        //Ran into too many snags knowing when the ItemsSource was loaded though (control just isn't flexible that way) 
        //so left the code in case other ideas may come up to handle the situation

        //public static DependencyProperty SelectedValueMemberPathProperty =
        //    DependencyProperty.Register("SelectedValueMemberPath", typeof(string), typeof(JobPlan.Controls.AutoCompleteComboBox), null);

        //public string SelectedValueMemberPath
        //{
        //    get
        //    {
        //        return ((string)(base.GetValue(AutoCompleteComboBox.SelectedValueMemberPathProperty)));
        //    }
        //    set
        //    {
        //        base.SetValue(AutoCompleteComboBox.SelectedValueMemberPathProperty, value);
        //    }
        //}

        //public static DependencyProperty SelectedValueProperty =
        //  DependencyProperty.Register("SelectedValue", typeof(object), typeof(JobPlan.Controls.AutoCompleteComboBox),
        //  new PropertyMetadata((o, e) =>
        //  {
        //      ((AutoCompleteComboBox)o).SetSelectionFromValue();
        //  }));

        //public object SelectedValue
        //{
        //    get
        //    {
        //        return ((object)(base.GetValue(AutoCompleteComboBox.SelectedValueProperty)));
        //    }
        //    set
        //    {
        //        base.SetValue(AutoCompleteComboBox.SelectedValueProperty, value);
        //    }
        //}

        public AutoCompleteComboBox()
        {
            this.DefaultStyleKey = typeof(AutoCompleteComboBox);
            Loaded += new RoutedEventHandler(AutoCompleteComboBox_Loaded);
            //this.SelectionChanged += new SelectionChangedEventHandler(AutoCompleteComboBox_SelectionChanged);
        }

        void AutoCompleteComboBox_Loaded(object sender, RoutedEventArgs e)
        {

        }

        //void AutoCompleteComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (e.AddedItems.Count > 0)
        //    {
        //        _Selection = e.AddedItems[0];
        //        object val = GetMemberValue(_Selection);
        //        SelectedValue = val;
        //    }
        //    else
        //    {
        //        _Selection = null;
        //        SelectedValue = null;
        //    }
        //}

        //private void SetSelectionFromValue()
        //{
        //    var value = SelectedValue;
        //    IList<object> items = ConvertItemsSource();
        //    if (items != null && items.Count > 0 && value != null)
        //    {
        //        var sel = (from item in items
        //                   where GetMemberValue(item).Equals(value)
        //                   select item).SingleOrDefault();
        //        _Selection = sel;
        //        SelectedItem = sel;
        //    }
        //}

        //private IList<object> ConvertItemsSource()
        //{
        //    if (this.ItemsSource != null)
        //    {
        //        List<object> newItems = new List<object>();
        //        foreach (var item in this.ItemsSource)
        //        {
        //            newItems.Add(item);
        //        }
        //        return newItems;
        //    }
        //    return null;
        //}

        //private object GetMemberValue(object item)
        //{
        //    if (!String.IsNullOrEmpty(SelectedValueMemberPath))
        //    {
        //        return item.GetType().GetProperty(SelectedValueMemberPath).GetValue(item, null);
        //    }
        //    return null;
        //}

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ToggleButton = GetTemplateChild(ElementToggleButton) as ToggleButton;
        }

        void ToggleButton_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "ToggleButtonOver", true);
        }

        void ToggleButton_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "ToggleButtonOut", true);
        }

        void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.SearchText))
            {
                this.Text = String.Empty;
            }
            this.IsDropDownOpen = !this.IsDropDownOpen;
        }
    }

    public class MyAutoCompleteBox : AutoCompleteBox
    {
        public static readonly DependencyProperty HandleKeyEventsProperty = DependencyProperty.Register(
        "HandleKeyEvents",
        typeof(bool),
        typeof(MyAutoCompleteBox),
        new PropertyMetadata(true));

        public bool HandleKeyEvents
        {
            get { return (bool)GetValue(HandleKeyEventsProperty); }
            set { SetValue(HandleKeyEventsProperty, value); }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (!this.HandleKeyEvents && e.Key == Key.Enter)
            {
                return;

            }
            base.OnKeyDown(e);
        }

        public MyAutoCompleteBox()
        {
            //this.DefaultStyleKey = typeof(MyAutoCompleteBox);
            this.ApplyTemplate();
        }

    }
}
